package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.ComExaminingReportMapper;
import com.bf.electroplating.pojo.dto.examiningreport.ComExaminingReportDTO;
import com.bf.electroplating.pojo.dto.examiningreport.ComExaminingReportQueryDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ComExaminingReport;
import com.bf.electroplating.pojo.enums.AttModuleEnums;
import com.bf.electroplating.pojo.enums.ReportTypeEnum;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.examiningreport.ComExaminingReportVO;
import com.bf.electroplating.service.ComCompanyService;
import com.bf.electroplating.service.ComExaminingReportService;
import com.bf.electroplating.service.PubAttachmentService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 15451
 * @description 针对表【com_examining_report(检查报告表)】的数据库操作Service实现
 * @createDate 2025-05-09 11:11:32
 */
@Service
public class ComExaminingReportServiceImpl extends ServiceImpl<ComExaminingReportMapper, ComExaminingReport>
        implements ComExaminingReportService {

    @Resource
    private ComCompanyService comCompanyService;
    @Resource
    private PubAttachmentService pubAttachmentService;

    @Override
    public ResultVO<Page<ComExaminingReportVO>> pageList(ComExaminingReportQueryDTO queryDTO) {
        Page<ComExaminingReport> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        LambdaQueryWrapper<ComExaminingReport> wrapper = new LambdaQueryWrapper<>();
        Map<String, String> companyMap = comCompanyService.list().stream().collect(Collectors.toMap(ComCompany::getId, ComCompany::getCompanyName));

        if (StringUtils.isNotBlank(queryDTO.getCompanyId())) {
            wrapper.eq(ComExaminingReport::getCompanyId, queryDTO.getCompanyId());
        }
        if (StringUtils.isNotBlank(queryDTO.getReportType())) {
            wrapper.eq(ComExaminingReport::getReportType, queryDTO.getReportType());
        }
        if (StringUtils.isNotBlank(queryDTO.getReportName())) {
            wrapper.like(ComExaminingReport::getReportName, queryDTO.getReportName());
        }
        wrapper.orderByDesc(ComExaminingReport::getCreateTime);

        Page<ComExaminingReport> resultPage = this.page(page, wrapper);

        // 转换为VO
        Page<ComExaminingReportVO> voPage = new Page<>();
        BeanUtils.copyProperties(resultPage, voPage, "records");

        List<ComExaminingReportVO> voList = new ArrayList<>();
        for (ComExaminingReport entity : resultPage.getRecords()) {
            ComExaminingReportVO vo = new ComExaminingReportVO();
            BeanUtils.copyProperties(entity, vo);
            vo.setReportType(ReportTypeEnum.getDesc(entity.getReportType()));
            vo.setCompanyName(companyMap.get(entity.getCompanyId()));
            vo.setAttachments(pubAttachmentService.getWholeFile(entity.getId(), AttModuleEnums.SELF_TESTING.getCode()));
            voList.add(vo);
        }
        voPage.setRecords(voList);

        return ResultVO.ok(voPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<String> saveData(ComExaminingReportDTO dto) {
        ComExaminingReport report = new ComExaminingReport();
        BeanUtils.copyProperties(dto, report);
        saveOrUpdate(report);

        pubAttachmentService.saveWholeAttachmentRel(dto.getAttachments(), report.getId(), AttModuleEnums.SELF_TESTING.getCode());

        return ResultVO.ok(null, "保存成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<String> delete(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return ResultVO.fail("ID不能为空");
        }

        this.removeByIds(ids);
        return ResultVO.ok(null, "删除成功");
    }

    @Override
    public ResultVO<ComExaminingReportVO> getDetail(String id) {
        if (StringUtils.isBlank(id)) {
            return ResultVO.fail("ID不能为空");
        }
        ComExaminingReport entity = this.getById(id);
        if (entity == null) {
            return ResultVO.fail("记录不存在");
        }
        Map<String, String> companyMap = comCompanyService.list().stream().collect(Collectors.toMap(ComCompany::getId, ComCompany::getCompanyName));
        ComExaminingReportVO vo = new ComExaminingReportVO();
        BeanUtils.copyProperties(entity, vo);
        vo.setCompanyName(companyMap.get(entity.getCompanyId()));
        vo.setAttachments(pubAttachmentService.getWholeFile(entity.getId(), AttModuleEnums.SELF_TESTING.getCode()));

        return ResultVO.ok(vo);
    }

}




